package com.zy.component;

import com.zy.bean.TAdmin;
import com.zy.bean.TAdminExample;
import com.zy.bean.TPermission;
import com.zy.bean.TRole;
import com.zy.mapper.TAdminMapper;
import com.zy.mapper.TPermissionMapper;
import com.zy.mapper.TRoleMapper;
import com.zy.service.TAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.management.relation.Role;
import java.security.Permission;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: zy 690748807@qq.com
 * @Description: TODO  安全框架验证用户名
 * @Date: 2024/2/18 22:13
 * @Version: 1.0
 * @name 方法名字：重写组件的实现类
 */
@Component
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    TAdminMapper tAdminMapper;
    @Autowired
    TRoleMapper tRoleMapper;
    @Autowired
    TPermissionMapper tPermissionMapper;



//    @Autowired
//    JdbcTemplate jdbcTemplate;
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        String sqr="SELECT * FROM t_admin WHERE loginacct=?";
//       // Map<String, Object> map=jdbcTemplate.queryForMap(sqr,username);
//
//        Map<String, Object> map = jdbcTemplate.queryForMap(sqr, username);
////2、将查询到的用户封装到框架使用的UserDetails里面
//
//        User user = new User(map.get("loginacct").toString(),  map.get("userpswd").toString(),
//                AuthorityUtils.createAuthorityList("ROLE_学徒", "ROLE_大师", "罗汉拳"));//暂时写死，过后数据库中查
//        return user;
//    }


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //1.查询admdin表
        TAdminExample tAdminExample = new TAdminExample();
        tAdminExample.createCriteria().andLoginacctEqualTo(username);
        List<TAdmin> tAdmins = tAdminMapper.selectByExample(tAdminExample);
        if (tAdmins.size() == 1 && tAdmins != null) {
            TAdmin tAdmin = tAdmins.get(0);
            //查询用户拥有的角色集合
            Integer adminId = tAdmin.getId();
            List<TRole> listrole = tRoleMapper.listRoleByAdminid(adminId);
            //查询用户拥有的权限集合
            List<TPermission> listpermission = tPermissionMapper.listPermissionByAdminid(adminId);
            //用户权限=【角色+权限】
            Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
            for (TRole tRole : listrole) {
                String rname = tRole.getName();
                authorities.add(new SimpleGrantedAuthority("ROLE_" + rname.toString()));

            }

            for (TPermission tPermission : listpermission) {
                String pname = tPermission.getName();
                authorities.add(new SimpleGrantedAuthority(pname));
            }

//            return new User(tAdmin.getLoginacct(),tAdmin.getUserpswd(), authorities);
            return  new TSecurityAdmin(tAdmin,authorities);
        } else {
            return null;
        }

    }
}
